package com.xdjd.task.service;

import com.xdjd.task.model.activity.ActivityClockDetailPo;
import com.xdjd.task.model.activity.ActivityUserClockPo;
import com.xdjd.task.service.daoservice.ActivityClockDetailPoService;
import com.xdjd.task.service.daoservice.ActivityUserClockPoService;
import com.xdjd.task.util.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class InitClockService {

    @Autowired
    private ActivityClockDetailPoService activityClockDetailPoService;
    @Autowired
    private ActivityUserClockPoService activityUserClockPoService;

    public void settleClockDetail() {
        activityClockDetailPoService.settleClockDetail();
    }

    public void initClockDetail() {
        activityUserClockPoService.endClock();
        List<ActivityUserClockPo> allOngoing = activityUserClockPoService.findAllOngoing();
        for (ActivityUserClockPo clockPo : allOngoing) {
            ActivityClockDetailPo clockDetailPo = activityClockDetailPoService.findByClockNoAndDate(clockPo.getClockNo(), DateUtil.getDate(new Date()));
            if (clockDetailPo == null) {
                if (clockPo.getRemainDay().intValue() > 0) {
                    clockDetailPo = new ActivityClockDetailPo();
                    clockDetailPo.setMemberId(clockPo.getMemberId());
                    clockDetailPo.setClockNo(clockPo.getClockNo());
                    clockDetailPo.setTargetStepNumber(clockPo.getDayStepNumber());
                    clockDetailPo.setTodayStepNumber(0l);
                    clockDetailPo.setStatus(new Byte("0"));
                    clockDetailPo.setCompleteStatus(new Byte("0"));
                    clockDetailPo.setClockDate(new Date());
                    activityClockDetailPoService.saveClockDetail(clockDetailPo);
                    activityUserClockPoService.reduceRemainDay(clockPo.getClockNo());
                }
            }
        }
    }
}
